/*
 * 作者:熊猫
 * 日期:2017 - 3 - 19 9 - 45 - 4
 *
 */

package net.lainiao.morethread.Chapter7.demo8;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/19.
 */
public class Main {
    public static void main(String[] args) {
        MyLock myLock=new MyLock();
        for (int i = 0; i < 10; i++) {
            Task task=new Task("Task-"+i,myLock);
            Thread thread=new Thread(task);
            thread.start();
        }
        boolean value=false;
        do{

            try {
                value=myLock.tryLock(1, TimeUnit.SECONDS);
                if(!value){
                    System.out.println("Main Trying get lock");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                value=false;
            }
        }while (!value);
        System.out.println("Main Got the lock");
        myLock.unlock();
        System.out.println("Main Over");
    }
}
